package com.suredy.base.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.base.model.Role;
import com.suredy.core.service.BaseSrvWithEntity;

@Service("RoleSrv")
public class RoleSrv extends BaseSrvWithEntity<Role> {
	@Override
	public DetachedCriteria getDc(Role t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (!StringUtils.isBlank(t.getGroupName())) {
			dc.add(Restrictions.eq("groupName", t.getGroupName()));
		}

		return dc;
	}
	
	public List<Role> getAll() {
		return this.readByEntity(null);
	}

	public Role getById(String roleId) {
		return this.get(roleId);
	}

	public Role getByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		Role search = new Role();
		search.setGroupName(name);
		Role r = this.readSingleByEntity(search);
		return r;
	}
}
